<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Core.php");
class CI_Article extends CI_Core{
	function __construct(){
		parent::__construct();
		$this->database('default');
		
	}
	//文章列表
	public function getlist($tj=array(),$tj2=array(),$web='admin',$pagesize=12,$page=1){
		$this->open('article');
		$data=$web=='admin'? $this->get_list($tj,$tj2):$this->front_get_list($tj,$tj2,$pagesize,$page);
		$arr=array();
		foreach($data['result'] as $v){
			$arr[]=array(
				'id'=>$v['id'],
				'title'=>$v['title'],
				'en_title'=>$v['en_title'],
				'remark'=>$v['remark'],
				'thumb'=>array_shift(json_decode($v['thumb'],true)),
				'views'=>$v['views'],
				'likes'=>$v['likes'],
				'category_id'=>$this->get_category($v['category_id']),
				'add_time'=>date('Y-m-d H:i:s',$v['add_time']),
				'intro'=>strip_tags($v['content']),
				'direct'=>$v['direct'],
				'wechat_urls'=>$v['wechat_urls'],
				'show_wechat'=>$v['show_wechat'],
			);
		} 
		ob_clean();
		$rs = array('total'=>$data['total_rows'],'rows'=>$arr);
	    return $this->status(true, $rs);
	}
	private function get_category($id){
		$a=$this->get_one('name', array('id'=>$id),'article_category');
		return $a['name'];
	}
	
	// 根据id获取信息
	public function getInfoById($id){
		if(empty($id)){
			return false;
		}
		return $this->get_one('*',array('id'=>$id),'article');
	}
	
	//根据分类ID查文章
	public function alllist($id,$arr=array()){
		$id=$id?$id:$this->ci->input->get('id');
		$arr = $this->get_all('id,thumb,title',array('category_id'=>$id),'article');
		return $this->status(true, $arr);
	}
	//自由查询文章
	public function pub_search($tj=array(),$tj2=array()){
		$arr = $this->get_all('id,thumb,title,en_title,add_time,remark',$tj,'article',$tj2);
		return $this->status(true, $arr);
	}
	//创建文章
	public function create(){
		
	}
	//删除文章
	public function remove(){
		$a=$this->get_all('id', array('no_del'=>1),'article',array('wherein'=>array('id'=>explode(',',$this->ci->input->post('id')))));
		$id=array_map('array_pop', $a);
		$this->db->where_in('id', $id);
		$this->db->delete('article');
	}
	//更新文章
	public function update($id){
		$params['id']=$id?$id:$this->ci->input->get('id');
		if(!empty($params['id']) && $params['id'] > 0){
			$id = $params['id'];
			$rs = $this->get_one('*',array('id'=>$id),'article');
			if(!$rs){
				return $this->status(false, '{"status":"ERROR","msg":"执行失败"}');
			}
		}
		if($rs['category_id']!=0){
			$arr = $this->get_one('*',array('id'=>$rs['category_id']),'article_category');
			$rs['cateName']=$arr['name'];
		}
		return $this->status(true, $rs);
	}
	//根据拼音查新闻
	public function getByPinyin($v){
		$rs = $this->get_one('*',array('en_title'=>$v),'article');
		return $this->status(true, $rs);
	}
	//简单保存
	public function textSave(){
		$row['content']=$this->ci->input->post('content');
		$this->write($row,'article');
	}
	//保存
	public function save(){
		$row = array(
			'title'       => $this->ci->input->post('title'),
			'en_title'       => $this->ci->input->post('en_title'),
			'keywords'    => $this->ci->input->post('keywords'),
			'description' => $this->ci->input->post('description'),
			'category_id' => $this->ci->input->post('category_id'),
			'author'      => $this->ci->input->post('author'),
			'source'      => $this->ci->input->post('source'),
			'direct'      => $this->ci->input->post('direct'),
			'content'     => $this->ci->input->post('content'),
			'show_pc'     => $this->ci->input->post('show_pc'),
			'show_app'    => $this->ci->input->post('show_app'),
			'show_wechat' => serialize($_POST['show_wechat']),
			'remark'      => $this->ci->input->post('remark'),
			'orderby'     => $this->ci->input->post('orderby'),
			'thumb'       => json_encode($this->ci->input->post('thumb')),
			'site_id'     => $this->ci->session->userdata('wechat_id')?$this->ci->session->userdata('wechat_id'):$this->ci->input->post('wechat_id'),
		);
		if($this->ci->input->post('id')==''){
			$row['add_time']=time();
			$row['site_id']='';
		}
		$id = $this->write($row,'article');
		$this->ci->load->library('Huashang/wechat');
		$this->ci->wechat->saveNewsToWechat($id,array(
			'thumb'  => $this->ci->input->post('thumb')?array_shift(json_decode($row['thumb'])):'uploads/image/space.jpg',
			'title'  => $row['title'],
			'intro'  => $row['remark'],
			'content'=> str_replace('../uploads',WEB.'../uploads',$row['content'])
		),$_POST['show_wechat']);
	}
	//读取图片
	public function getPic($tj=array(),$tj2=array(),$web='admin',$pagesize=12,$page=1){
		$this->open('pic');
		$data=$web=='admin'? $this->get_list($tj,$tj2):$this->front_get_list($tj,$tj2,$pagesize,$page);
		$rs = array('total'=>$data['total_rows'],'rows'=>$data['result']);
	    return $this->status(true, $rs);
	}
	//根据ID获取
	public function getPicById($id){
		$a=$this->get_one('*',array('id'=>$id),'pic');
		return $this->status(true,$a);
	}
//自由查询图片
	public function pubPic($tj=array(),$tj2=array()){
		$arr = $this->get_all('*',$tj,'pic',$tj2);
		return $this->status(true, $arr);
	}
//保存图片
	public function savePic($v){
		$row['type']=$v['type'];
		$row['title']=$v['title'];
		$row['pic']=$v['pic'];
		$row['pc_url']=$v['pc_url'];
		$row['wap_url']=$v['wap_url'];
		if($data['id']==''){
			$row['add_time']=time();
		}
		$this->write($row,'pic');
	}
	//删除图片
	public function delPic($id){
		if(!$id) return 'error';
		$id=explode(',', $id);
		$this->db->where_in('id', $id);
		$this->db->delete('pic');
	}
}